package constant.milk.cashbook.house;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.asset.CountActivity;
import constant.milk.cashbook.bean.AptData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbDeleteQuery;
import constant.milk.cashbook.database.DbInsertQuery;
import constant.milk.cashbook.database.TableName;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkUtil;

public class HouseInsertActivity extends SuperActivity
{
	private static final int COUNT_REQUEST_CODE = 0;
	
	public static final String HOUSE_ID = "HOUSE_ID";
	
	private View meibView;
	private View junseView;
	
	private TextView areaTextView;
	private TextView pungMoneyTextView;
	private TextView totalMoneyTextView;
	
	private View pungView;
	private TextView pungTextView;
	
	private Button saveButton;
	
	private DBSelectQuery select;
	
	private int meibJunseFlag;
	
	private AptData data;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.house_insert_activity);
		
		setTitle("집 설정");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		meibView = findViewById(R.id.houseInsertMeibView);
		junseView = findViewById(R.id.houseInsertJunseView);
		
		areaTextView = (TextView) findViewById(R.id.houseInsertAreaTextView);
		pungMoneyTextView = (TextView) findViewById(R.id.houseInsertPungMoneyTextView);
		totalMoneyTextView = (TextView) findViewById(R.id.houseInsertTotalMoneyTextView);
		
		pungView = findViewById(R.id.houseInsertPungView);
		pungTextView = (TextView) findViewById(R.id.houseInsertPungTextView);
		
		saveButton = (Button) findViewById(R.id.saveButton);
	}

	@Override
	protected void setOnClick()
	{
		meibView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setMeib();
			}
		});
		
		junseView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setJunse();
			}
		});
		
		pungView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CountActivity.class);
				intent.putExtra(CountActivity.COUNT_DATA, pungTextView.getText().toString());
				intent.putExtra(CountActivity.COUNT_FLAG, 3);
				startActivityForResult(intent, COUNT_REQUEST_CODE);
			}
		});
		
		saveButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (setCheckData())
				{
					return;
				}
				
				if (setSaveData())
				{
					MilkUtil.showDialogFinish(superContext, "확인", "데이터 저장에 성공하였습니다.");
				}
				else
				{
					MilkUtil.showDialog(superContext, "실패", "잠시후 다시해 주세요.");
				}
			}
		});
	}
	
	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		Intent intent = getIntent();
		String id = intent.getStringExtra(HOUSE_ID);
		
		data = select.getHouseId(id);
		
		areaTextView.setText(data.getTitle() + " " + data.getName());
		
		meibJunseFlag = 0;
		
		pungTextView.setText("20");
		
		setMeib();
	}
	
	private boolean setCheckData()
	{
		if (areaTextView.getText().toString().equals(""))
		{
			MilkUtil.showDialog(superContext, "확인", "지역이 올바르지 않습니다.");
			return true;
		}
		
		if (MilkUtil.getCommaDel(pungMoneyTextView.getText().toString()).equals("0"))
		{
			MilkUtil.showDialog(superContext, "확인", "최근 거래가 없어서 평당 금액이 0원입니다. 0원인 곳은 선택 할 수 없습니다.");
			return true;
		}
		
		if (MilkUtil.getCommaDel(totalMoneyTextView.getText().toString()).equals("0"))
		{
			MilkUtil.showDialog(superContext, "확인", "집가격 0원은 저장 할 수 없습니다.");
			return true;
		}
		
		return false;
	}
	
	private boolean setSaveData()
	{
		DbInsertQuery insertQuery = null;
		
		try
		{
			superDb.delete(superContext, TableName.MY_HOUSE, null, null);
			insertQuery = new DbInsertQuery(superContext, superDb);
			
			insertQuery.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("title", data.getTitle());
			map.put("name", data.getName());
			
			map.put("flag", meibJunseFlag + "");
			
			map.put("pung", pungTextView.getText().toString());
			
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			
			map.put("date", MilkUtil.getDateDB(year, month+1, day));
			
			insertQuery.setMyHouse(map);
			
			insertQuery.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				insertQuery.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	private void setTotalMoney()
	{
		BigDecimal money = new BigDecimal(MilkUtil.getCommaDel(pungMoneyTextView.getText().toString()));
		
		BigDecimal totalMoney = new BigDecimal(money.multiply(new BigDecimal(pungTextView.getText().toString())).intValue());
		
		totalMoneyTextView.setText(MilkUtil.getWon(totalMoney.toString()));
	}
	
	private void setPungData()
	{
		BigDecimal money = null;
		
		if (meibJunseFlag == 0)
		{
			money = new BigDecimal(data.getMeib() + "0000");
		}
		else
		{
			money = new BigDecimal(data.getJunse() + "0000");
		}
		
		BigDecimal pungMoney = new BigDecimal(money.multiply(new BigDecimal(3.3)).intValue());
		
		pungMoneyTextView.setText(MilkUtil.getWon(pungMoney.toString()));
	}
	
	private void setMeib()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		meibView.setBackgroundResource(R.drawable.btn_neon_push);
		meibView.setPadding(padding, padding, padding, padding);
		junseView.setBackgroundResource(0);
		junseView.setPadding(padding, padding, padding, padding);
		
		meibJunseFlag = 0;
		
		setPungData();
		
		setTotalMoney();
	}
	
	private void setJunse()
	{
		int padding = MilkUtil.dipToPixel(superContext, 10);
		meibView.setBackgroundResource(0);
		meibView.setPadding(padding, padding, padding, padding);
		junseView.setBackgroundResource(R.drawable.btn_neon_push);
		junseView.setPadding(padding, padding, padding, padding);
		
		meibJunseFlag = 1;
		
		setPungData();
		
		setTotalMoney();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == COUNT_REQUEST_CODE)
		{
			String str = data.getStringExtra(CountActivity.COUNT_DATA);
			
			pungTextView.setText(str);
			
			setTotalMoney();
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_MY_HOUSE);
		startActivity(intent);
	}
}
